package cs2340.klondike.TaskFudger;

import java.util.ArrayList; 
import java.util.List;

import com.j256.ormlite.dao.ForeignCollection;

import models.Category;
import models.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * One the user clicks on create category, this class is loaded as the main Activity where all the categories are displayed.
 * @version 1.0
 */
public class CategoriesActivity extends Activity {
	private ListView categoriesList;
	private ArrayAdapter<String> adapter;
	List<Category> categories;
	ArrayList<String> categoriesAsStrings;
	Category selectedCategory;
	ForeignCollection<Category> categoriesArray;
	
	/**
	 * This method sets the category for the current user.
	 */
	private void setCategoryList(){
		categoriesArray = User.getCurrentUser().getCategories();
		
		categoriesAsStrings = new ArrayList<String>();
       	categories = new ArrayList<Category>();
        for (Category c : categoriesArray) {
        	categories.add(c);
        	categoriesAsStrings.add(c.toString());
        }
        
        adapter = new ArrayAdapter<String>(CategoriesActivity.this, android.R.layout.simple_list_item_1, categoriesAsStrings);
        categoriesList = (ListView) findViewById(R.id.categoriesList); 
        categoriesList.setAdapter(adapter);
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.categories);
		categoriesList = (ListView)findViewById(R.id.categoriesList);
        Button newCategory = (Button) findViewById(R.id.newCategory);
		setCategoryList();        
        categoriesList.setOnItemClickListener(
        		new OnItemClickListener(){
        			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        				// TODO Auto-generated method stub
        				categoriesList.setSelection(position);
        				selectedCategory = categories.get(position);
        				Log.i("EDITING", categoriesAsStrings.get(position));
        				
        				try {
        						Intent intent = new Intent(view.getContext(), NewCategoryActivity.class);
        						intent.putExtra("CATEGORY_ID", selectedCategory.extractId());
        						startActivity(intent);
        						setCategoryList();
        				}catch (Exception e) {
        							// TODO Auto-generated catch block
        							e.printStackTrace();
        				}
        			}
         		}
        );
        
        
        newCategory.setOnClickListener(new View.OnClickListener() {
			
			/* (non-Javadoc)
			 * @see android.view.View.OnClickListener#onClick(android.view.View)
			 */
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), NewCategoryActivity.class);
		        startActivity(intent);
		        finish();
			}
		});
	}
}
